
const { EquipmentModel, CabinetModel, EquipmentLogModel } = require('../../models/index')
const koaTool = require('../../commons/koa_tool')
const ObjectId = require("mongoose").Types.ObjectId
const moment = require("moment")

class RptEnterController {

    // 获取设备名称
    getEquDetailById = async (ctx) => {
        let [checkError, input] = koaTool.checkInput(ctx, ["eId"])
        if (checkError) {
            return koaTool.fail(ctx, checkError)
        }

        let equipData = await EquipmentModel.findById(input.eId).populate({path: "cabinet_id", select: "name code"}).lean();
        let result = {
            equName: `${equipData.name}(${equipData.code})`,
            cabName: `${equipData.cabinet_id.name}(${equipData.cabinet_id.code})` 
        };

        koaTool.success(ctx, "获取成功", result)
    }
    
    // 获取柜数据
    getCabinetDataList = async (ctx) => {
        let [checkError, input] = koaTool.checkInput(ctx, [])
        if (checkError) {
            return koaTool.fail(ctx, checkError)
        }

        let enterId = ctx.state._user.enterprise_id;
        
        let query = {
            enterprise_id: enterId,
            status: 1,
        }
        let cabinetDataList = await CabinetModel.find(query, "_id name code cabinet_status").sort({_id: -1}).lean();

        let resultDataList = [];

        let allPro = cabinetDataList.map(async item => {
            
            let equQuery = {
                enterprise_id: enterId,
                cabinet_id: item._id,
                status: 1
            }
            let resultData = {
                cabId: item._id,
                cabName: `${item.name} (${item.code})`,
                status: item.cabinet_status,
                equTemp: {
                    total: 0,
                    active: 0
                },
                equGas: {
                    total: 0,
                    active: 0
                }
            }

            let equDataList = await EquipmentModel.find(equQuery, "type machine_status").lean();
            equDataList.forEach(eItem => {
                let equProp = eItem.type === 1 ? 'equTemp' : 'equGas'
                resultData[equProp].total += 1;
                resultData[equProp].active += (eItem.machine_status === 1 ? 1 : 0);
            })

            resultDataList.push(resultData);
        })

        await Promise.all(allPro);

        koaTool.success(ctx, "获取成功", {
            list: resultDataList
        })

    }

    // 获取柜内数据信息
    getEquDataList = async (ctx) => {
        let [checkError, input] = koaTool.checkInput(ctx, ["cabId"])
        if (checkError) {
            return koaTool.fail(ctx, checkError)
        }

        let query = {
            status: 1,
            cabinet_id: ObjectId(input.cabId)
        }

        let equDataList = await EquipmentModel.find(query, "name code type gas_type real_num machine_status cordon").sort({type: 1}).lean();
        
        koaTool.success(ctx, "获取成功", {
            list: equDataList
        })

    }

    // 获取设备最新数据 -30条
    getEquLogDataList = async (ctx) => {
        let [checkError, input] = koaTool.checkInput(ctx, ["equId"])
        if (checkError) {
            return koaTool.fail(ctx, checkError)
        }

        let query = {
            status: 1,
            equipment_id: ObjectId(input.equId),
        }
        let logDataList = await EquipmentLogModel.find(query, 'real_num create_at').sort({_id: -1}).limit(30).lean();
        let resultData = logDataList.map(item => {
            return {
                value: item.real_num,
                time: moment(item.create_at).format("HH:mm:ss")
            }
        })
        koaTool.success(ctx, "获取成功", {
            list: resultData
        })

    }

    // 查询前15条异常数据
    getWarningDataList = async (ctx) => {

        let enterId = ctx.state._user.enterprise_id;
        let query = {
            status: 1,
            enterprise_id: enterId,
            num_status: 2
        }
        let commonQuery = {
            status: 1,
            enterprise_id: enterId,
        }
        let logDataList = await EquipmentLogModel.find(query).sort({_id: -1}).limit(8).lean();
        let cabinetDataList = await CabinetModel.find(commonQuery, 'name').lean();
        let equipmentDataList = await EquipmentModel.find(commonQuery, 'name').lean();
        
        let cabinetEnum = {}, equEnum = {};
        cabinetDataList.forEach(item => {
            cabinetEnum[item._id] = item.name
        });
        equipmentDataList.forEach(item => {
            equEnum[item._id] = item.name
        });


        let resultData = logDataList.map(item => {
            return {
                cabId: item.cabinet_id,
                cabName: cabinetEnum[item.cabinet_id],
                equId: item.equipment_id,
                equName: equEnum[item.equipment_id],
                datetime: moment(item.create_at).format("MM-DD HH:mm"),
                value: item.real_num
            }
        })


        koaTool.success(ctx, "获取成功", {
            list: resultData
        })

    }

}

module.exports=new RptEnterController()